import util.StringUtil;

import java.util.ArrayList;

/**
 * Created by gamy on 2017/7/15.
 */
public class TableProperty {
    String tableName;
    String packageName;
    String classFilePath;

    final static public String COLUMN_TYPE_INT = "INT";
    final static public String COLUMN_TYPE_TEXT = "TEXT";
    final static public String COLUMN_TYPE_FLOAT = "FLOAT";
    final static public String COLUMN_TYPE_CHAR = "CHAR";
    final static public String COLUMN_TYPE_BYTE = "TINYINT";
    final static public String COLUMN_TYPE_DATETIME = "DATETIME";

    ArrayList<TableColumn> columns = new ArrayList<TableColumn>();

    public static String covertType(String type) {
        if (type.contains(COLUMN_TYPE_BYTE)) {
            return "int";
        }
        if (type.contains(COLUMN_TYPE_INT)) {
            return "long";
        }

        if (type.contains(COLUMN_TYPE_FLOAT)) {
            return "float";
        }

        if (type.contains(COLUMN_TYPE_TEXT)) {
            return "String";
        }

        if (type.contains(COLUMN_TYPE_CHAR)) {
            return "String";
        }

        if (type.contains(COLUMN_TYPE_DATETIME)) {
            return "java.sql.Date";
        }
        return "";
    }

    @Override
    public String toString() {
        String s = "table:" + tableName + "\n";
        for (TableColumn column : this.columns) {
            s += column.toString() + "\n";
        }
        return s + "\n";
    }

    TableProperty(String tableName) {
        this.tableName = tableName;
        this.packageName = "com.st.dao." + tableName.toLowerCase();
        this.classFilePath = "/Users/gamy/projects/java/HeyJersey/src/com/st/dao/" + tableName.toLowerCase() + "/" + tableName + ".java";
    }

    static public class TableColumn {
        boolean isPK;
        String column;
        String type;
        String property;


        TableColumn(String lineStr) {
            column = StringUtil.getColumnName(lineStr);
            type = covertType(lineStr);
            property = StringUtil.underline2Camel(column, true);
            if (lineStr.contains("PRIMARY KEY")) {
                isPK = true;
            }
        }

        @Override
        public String toString() {
            return "(" + column + ", " + property + ", " + isPK + ", " + type + ")";
        }
    }
}

